package com.dqyt.app.mongo;

import com.dqyt.app.bean.Report;
import com.dqyt.app.bean.Sample;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 报告dao
 */
@Component
public class ReportDao {

    /**
     * 注入MongoTemplate
     */
    @Autowired
    protected MongoTemplate mongoTemplate;

    /**
     * 新建数据到数据库
     *
     * @return
     */
    public void save(Report report) {
        mongoTemplate.save(report); // 保存数据到数据库
    }

    /**
     * 删除
     * @param report
     */
    public void delete(Report report){
        mongoTemplate.remove(report);
    }


    /**
     * 删除
     * @param bh
     */
    public long deleteByBh(String bh){
        Query query=new Query(Criteria.where("bh").is(bh));
        long count = mongoTemplate.remove(query,Report.class).getDeletedCount();
        return count;
    }




    /**
     * 查询报告列表信息分页
     *
     * @param addTime
     * @param reportStatue
     * @param bsbh
     * @param erweima
     * @return
     */
    public List<Report> getReportInfos(int pageNum, int pageSize,long addTime, int reportStatue, String bsbh, String erweima,int type,int reportProgress,long wtfId) {
        // 构建查询条件
        Query query = new Query();

       // query = Query.query(Criteria.where("addTime").lt(addTime)).limit(10).with(Sort.by(Sort.Direction.DESC, "addTime"));

        if (reportStatue != 0) {
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }

        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }



        if (type != 0) {
            query.addCriteria(new Criteria("type").is(type));
        }

        if (reportProgress != 0) {
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        if(wtfId !=0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        query.with(Sort.by(Sort.Order.desc("pxzd"),Sort.Order.desc("bsbh")));
        query.limit(pageSize).skip(pageNum);

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }

    /**
     * 查询某条数据的详细信息
     *
     * @param type
     * @param bh
     * @return
     */
    public List<Report> getReport(int type, String bh, String erweima) {
        // 构建查询条件
        Query query = new Query();

        if (type != 0) {
            query.addCriteria(new Criteria("type").is(type));
        }

        if (null != bh && !bh.equals("")) {
            query.addCriteria(new Criteria("bh").is(bh));
        }

        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        query.limit(10).with(Sort.by(Sort.Direction.DESC, "addTime"));

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }

    /**
     * 根据 任务单编号和类型查询 已完成的报告
     * @param rwdbh
     * @param type
     * @return
     */
    public List<Report> getReportByrwdbh(String rwdbh,int type){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("type").is(type));
        query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        query.addCriteria(new Criteria("reportProgress").is(4));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     * 询某条数据的详细信息
     * @param bh
     * @return
     */
    public Report getReportByBH(String bh) {
        // 构建查询条件
        Query query = new Query(new Criteria("bh").is(bh));
        Report ret = mongoTemplate.findOne(query, Report.class);
        return ret;
    }


    /**
     * 询某条数据的详细信息
     * @param bsbh
     * @return
     */
    public Report getReportBybsbh(String bsbh) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("bsbh").is(bsbh));
        query.with(Sort.by(Sort.Order.asc("addTime")));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     * 询某条数据的详细信息
     * @param bsbh
     * @return
     */
    public Report getReportBybsbh(String bsbh,int type) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("bsbh").is(bsbh));
        query.addCriteria(new Criteria("type").is(type));
        query.with(Sort.by(Sort.Order.asc("addTime")));

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }



    /**
     * 询某条数据的详细信息
     * @param
     * @return
     */
    public Report getReportBybsbhOrsb(String bsbh,String sbbh,int type) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("bsbh").is(bsbh));
        query.addCriteria(new Criteria("sbbh").is(sbbh));
        query.addCriteria(new Criteria("type").is(type));
        query.with(Sort.by(Sort.Order.asc("addTime")));

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }


    /**
     * 查询 当前气瓶被那些面罩 匹配
     * @param jyrqNian
     * @param qpbsh
     * @param type
     * @param bsbh  员气瓶信息
     * @return
     */
    public  List<Report>  getMoreMzByPar(String jyrqNian,String qpbsh,int type,String bsbh){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        query.addCriteria(new Criteria("type").is(type));
        query.addCriteria(new Criteria("respirator.qpbsh").is(qpbsh));
//        query.addCriteria(new Criteria("bsbh").ne(bsbh));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        return list;
    }



    /**
     * 询某条数据的详细信息
     * @param bsbh
     * @return
     */
    public Report getReportBybsbhAdType(String bsbh,int type) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("bsbh").is(bsbh));
        query.addCriteria(new Criteria("type").is(type));

        query.with(Sort.by(Sort.Order.asc("addTime")));

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }


    /**
     * 询某条数据的详细信息
     * @param bsbh
     * @return
     */
    public Report getReportBySbbhAdType(String bsbh,int type) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("bsbh").is(bsbh));
        query.addCriteria(new Criteria("type").is(type));

        query.with(Sort.by(Sort.Order.asc("addTime")));

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }


    /**
     * 分页查询 我的报告
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 报告状态 1：待检验  2：待核验 3：待批准  4：合格  5：不合格  6：驳回
     * @param wtfId  委托方id
     * @param spType  送检类型
     * @return
     */
    public List<Report>  getReportListPage(int pageNum, int pageSize, String bsbh, String erweima, int type, int reportStatue,
                                             int wtfId, int spType,int sysStatus,int zzcsId,String jyyId,int reportProgress,String jyrqNian,String jyrqYue,String rwdbh,String sbbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(spType !=0 ){
            query.addCriteria(new Criteria("spType").is(spType));
        }
        if (null != sbbh && !sbbh.equals("")) {
            query.addCriteria(new Criteria("sbbh").is(sbbh));
        }

        if(zzcsId != -1 ){
            query.addCriteria(new Criteria("zzcsId").is(String.valueOf(zzcsId)));
        }
        if(reportProgress !=0 ){
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        if(reportStatue !=0 ){
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != jyrqNian && !jyrqNian.equals("")) {
            query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        }

        if (null != jyrqYue && !jyrqYue.equals("")) {
            query.addCriteria(new Criteria("jyrqYue").is(jyrqYue));
        }

        if(null != jyyId && !jyyId.equals("")){
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }

        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }
        query.with(Sort.by(Sort.Order.desc("pxzd"),Sort.Order.desc("bsbh")));
        query.limit(pageSize).skip(pageNum);

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据

        return list;
    }




    /**
     * 查询总条数  我的报告
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 报告状态 1：待检验  2：待核验 3：待批准  4：合格  5：不合格  6：驳回
     * @param wtfId  委托方id
     * @param spType  送检类型
     * @return
     */
    public long getReportListTotle(String bsbh, String erweima, int type, int reportStatue,
                                     int wtfId, int spType,int sysStatus,int zzcsId,String jyyId,int reportProgress,String jyrqNian,String jyrqYue,String rwdbh,String sbbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(spType !=0 ){
            query.addCriteria(new Criteria("spType").is(spType));
        }

        if (null != sbbh && !sbbh.equals("")) {
            query.addCriteria(new Criteria("sbbh").is(sbbh));
        }

        if(zzcsId != -1 ){
            query.addCriteria(new Criteria("zzcsId").is(String.valueOf(zzcsId)));
        }
        if(reportProgress !=0 ){
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        if(reportStatue !=0 ){
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != jyrqNian && !jyrqNian.equals("")) {
            query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        }

        if (null != jyrqYue && !jyrqYue.equals("")) {
            query.addCriteria(new Criteria("jyrqYue").is(jyrqYue));
        }
        if(null != jyyId && !jyyId.equals("")){
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }

        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }


        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list.size();
    }






    /**
     * 分页查询 我的报告
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 报告状态 1：待检验  2：待核验 3：待批准  4：合格  5：不合格  6：驳回
     * @param wtfId  委托方id
     * @param spType  送检类型
     * @return
     */
    public List<Report>  getReportListPage2(int pageNum, int pageSize, String bsbh, String erweima, int type, int reportStatue,
                                           int wtfId, int spType,int sysStatus,int zzcsId,String jyyId,int reportProgress,String jyrqNian,String jyrqYue,String rwdbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(spType !=0 ){
            query.addCriteria(new Criteria("spType").is(spType));
        }

        if(zzcsId != -1 ){
            query.addCriteria(new Criteria("zzcsId").is(String.valueOf(zzcsId)));
        }


        if(reportStatue !=0 ){
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != jyrqNian && !jyrqNian.equals("")) {
            query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        }

        if (null != jyrqYue && !jyrqYue.equals("")) {
            query.addCriteria(new Criteria("jyrqYue").is(jyrqYue));
        }

        if(null != jyyId && !jyyId.equals("")){
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }

        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        query.with(Sort.by(Sort.Order.desc("pxzd"),Sort.Order.desc("bsbh")));
        query.limit(pageSize).skip(pageNum);

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }




    /**
     * 查询总条数  我的报告
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 报告状态 1：待检验  2：待核验 3：待批准  4：合格  5：不合格  6：驳回
     * @param wtfId  委托方id
     * @param spType  送检类型
     * @return
     */
    public long getReportListTotle2(String bsbh, String erweima, int type, int reportStatue,
                                   int wtfId, int spType,int sysStatus,int zzcsId,String jyyId,int reportProgress,String jyrqNian,String jyrqYue,String rwdbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(spType !=0 ){
            query.addCriteria(new Criteria("spType").is(spType));
        }

        if(zzcsId != -1 ){
            query.addCriteria(new Criteria("zzcsId").is(String.valueOf(zzcsId)));
        }


        if(reportStatue !=0 ){
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != jyrqNian && !jyrqNian.equals("")) {
            query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        }

        if (null != jyrqYue && !jyrqYue.equals("")) {
            query.addCriteria(new Criteria("jyrqYue").is(jyrqYue));
        }
        if(null != jyyId && !jyyId.equals("")){
            query.addCriteria(new Criteria("jyyId").is(jyyId));
        }

        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list.size();
    }



    /**
     * 分页查询 我的报告
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 报告状态 1：待检验  2：待核验 3：待批准  4：合格  5：不合格  6：驳回
     * @param wtfId  委托方id
     * @param spType  送检类型
     * @return
     */
    public List<Report>  getMyReportListPage(int pageNum, int pageSize, String bsbh, String erweima, int type, int reportStatue,
                                             int wtfId,int wtfIdChi, int spType,int sysStatus,int zzcsId,String jyyId,
                                             int reportProgress,String jyrqNian,String jyrqYue,String rwdbh,String sbbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }

        if(wtfIdChi>0){
            query.addCriteria(new Criteria("wtfIdChi").is(wtfIdChi));
        }

        if(null != sbbh && !"".equals(sbbh)){
            query.addCriteria(new Criteria("sbbh").is(sbbh));
        }

        if(spType !=0 ){
            query.addCriteria(new Criteria("spType").is(spType));
        }

        if(zzcsId != -1 ){
            query.addCriteria(new Criteria("zzcsId").is(String.valueOf(zzcsId)));
        }
        if(reportProgress !=0 ){
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        if(reportStatue !=0 ){
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != jyrqNian && !jyrqNian.equals("")) {
            query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        }

        if (null != jyrqYue && !jyrqYue.equals("")) {
            query.addCriteria(new Criteria("jyrqYue").is(jyrqYue));
        }

        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }


        query.addCriteria(new Criteria("jyyId").is(jyyId));
        query.with(Sort.by(Sort.Order.desc("pxzd"),Sort.Order.desc("bsbh")));
        query.limit(pageSize).skip(pageNum);

        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     * 查询总条数  我的报告
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param reportStatue 报告状态 1：待检验  2：待核验 3：待批准  4：合格  5：不合格  6：驳回
     * @param wtfId  委托方id
     * @param spType  送检类型
     * @return
     */
    public long getMyReportListTotle(String bsbh, String erweima, int type, int reportStatue,
                                     int wtfId,int wtfIdChi, int spType,int sysStatus,int zzcsId,String jyyId,int reportProgress,String jyrqNian,String jyrqYue,String rwdbh,String sbbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }

        if(wtfIdChi>0){
            query.addCriteria(new Criteria("wtfIdChi").is(wtfIdChi));
        }

        if(null != sbbh && !"".equals(sbbh)){
            query.addCriteria(new Criteria("sbbh").is(sbbh));
        }

        if(spType !=0 ){
            query.addCriteria(new Criteria("spType").is(spType));
        }

        if(zzcsId != -1 ){
            query.addCriteria(new Criteria("zzcsId").is(String.valueOf(zzcsId)));
        }
        if(reportProgress !=0 ){
            query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        }

        if(reportStatue !=0 ){
            query.addCriteria(new Criteria("reportStatue").is(reportStatue));
        }

        if (null != jyrqNian && !jyrqNian.equals("")) {
            query.addCriteria(new Criteria("jyrqNian").is(jyrqNian));
        }

        if (null != jyrqYue && !jyrqYue.equals("")) {
            query.addCriteria(new Criteria("jyrqYue").is(jyrqYue));
        }

        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }



        query.addCriteria(new Criteria("jyyId").is(jyyId));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list.size();
    }



    /**
     * 查询N日新增数量
     * @param mtime
     * @return
     */
    public List<Report> befaultxDate(long mtime) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("addTime").gt(mtime));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }




    /**
     * 查询N日审核报告
     * @param mtime
     * @return
     */
    public List<Report> befaultxDateSh(long mtime) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("addTime").gt(mtime));
        Criteria criteria = new Criteria();
        //查询报告状态为合格或者不合格的
        criteria.orOperator(Criteria.where("reportProgress").is(4));
        query.addCriteria(criteria);
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }

    /**
     *
     * @param wtfId
     * @param calculation
     * @return
     */
    public List<Report> getReportOverdue(String wtfId,long calculation,String wtfIdChi){
        // 构建查询条件
        Query query = new Query();

        if(!"0".equals(wtfId)){
            //委托方等于
            query.addCriteria(new Criteria("wtfId").is(Integer.parseInt(wtfId)));
        }
        //报告状态是已经完成的
        query.addCriteria(new Criteria("reportProgress").is(4));

        if(!"0".equals(wtfIdChi)){
            query.addCriteria(new Criteria("wtfIdChi").is(Integer.parseInt(wtfIdChi)));
        }

        Criteria criteria2 = new Criteria();
        long timeMillis = TimeUtil.currentTimeMillis();
        //计算时间 大于当前时间  并且 计算时间小于下次检验日期
        criteria2.andOperator(Criteria.where("xcjyrq").gt(timeMillis),Criteria.where("xcjyrq").lt(calculation));
        //检测人等于
//        criteria2.orOperator(Criteria.where("respirator.sjrId").is(sjrId), Criteria.where("cylinders.sjrId").is(sjrId));
        //除去压力表的
        criteria2.norOperator(Criteria.where("type").is(3));

        query.addCriteria(criteria2);

        query.with(Sort.by(Sort.Order.asc("isSendSMS")));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }

    public void batchUpdate(String [] smapIds){
       // JSONArray ids= JSON.parseArray(smapIds);
        // 构建查询条件
//        Query query = new Query();
//        Criteria where = new Criteria();
//        where.and("_id").in(smapIds);
//        Update update = new Update();
//        update.set("isSendSMS", 1);
//        mongoTemplate.upsert(query, update, "isSendSMS");





    }

    public void batchUpdate2(String bh){
        Query query = new Query(new Criteria("bh").is(bh));
        Update update = new Update();
        update.set("isSendSMS", 1);


        mongoTemplate.upsert(query, update, "Report");



    }


    public long getCount(){
        Query query = new Query();
        long count = mongoTemplate.count(query, Report.TableName);
        return count;
    }


    public long getCountAll(){
        Query query = new Query();
        long count = mongoTemplate.count(query, Report.TableName);
        return count;
    }

    public long getCountBhq(){
        Query query = new Query();
        //报告状态是已经完成的
        query.addCriteria(new Criteria("reportStatue").is(2));
        long count = mongoTemplate.count(query, Report.TableName);
        return count;
    }



    /**
     * 查询这批任务单号中 有多少气瓶是充了气的  并且报告已经完成了的
     * @param rwdbh
     * @return
     */
    public List<Report> getReportByrwdbhOrCq(String rwdbh){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("type").is(2));
        query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        query.addCriteria(new Criteria("reportProgress").is(4));
        query.addCriteria(new Criteria("cylinders.sfcq").is(1));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;

    }




    /**
     * 根据  更具类型的 标识编号的id列表批量擦汗寻 已完成的报告  用于工作量统计单
     * @param bsbhList  标识编号列表
     * @param type
     * @return
     */
    public List<Report> getReportByBatch(List<String> bsbhList,int type){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("type").is(type));
        query.addCriteria(Criteria.where("bsbh").in(bsbhList));
        query.addCriteria(new Criteria("reportProgress").is(4));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;
    }



    /**
     * 有多少气瓶是充了气的
     * 查询在这些 标识编号列表 里面  是充气的  并且报告已经完成了
     * @return
     */
    public List<Report> getReportByrwdbhOrCqBath(List<String> bsbhList){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("type").is(2));
        query.addCriteria(Criteria.where("bsbh").in(bsbhList));
        query.addCriteria(new Criteria("reportProgress").is(4));
        query.addCriteria(new Criteria("cylinders.sfcq").is(1));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list;

    }


    /**
     * 任务统计单 分页查询
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @param startTime
     * @param endTime
     * @param jyyId
     * @return
     */
    public List<Report>  getReportListPage4(int pageNum, int pageSize, String rwdbh,long wtfId,long startTime,long endTime,long jyyId){
        // 构建查询条件
        Query query = new Query();


        if (!"".equals(rwdbh) && !"-1".equals(rwdbh)) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        if (!"".equals(wtfId) && !"-1".equals(wtfId)) {
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }

        if(startTime!=0){
            query.addCriteria(new Criteria("jsrqLong").gt(startTime));
        }

        if(endTime!=0){
            query.addCriteria(new Criteria("jsrqLong").lt(startTime));
        }
        if(jyyId !=0){
            query.addCriteria(new Criteria("jsrqLong").lt(startTime));
        }

        Criteria criteria2 = new Criteria();
        //检测人等于
        criteria2.orOperator(Criteria.where("respirator.jyyId").is(jyyId), Criteria.where("cylinders.jyyId").is(jyyId));
        query.addCriteria(criteria2);

        query.limit(pageSize).skip(pageNum);
        query.with(Sort.by(Sort.Order.desc("addTime")));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据

        return list;
    }


    /**
     * 任务统计单 分页查询
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @param startTime
     * @param endTime
     * @param jyyId
     * @return
     */
    public long getReportListTotle4(int pageNum, int pageSize, String rwdbh,long wtfId,long startTime,long endTime,long jyyId){
        // 构建查询条件
        Query query = new Query();


        if (!"".equals(rwdbh) && !"-1".equals(rwdbh)) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        if (!"".equals(wtfId) && !"-1".equals(wtfId)) {
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }

        if(startTime!=0){
            query.addCriteria(new Criteria("jsrqLong").gt(startTime));
        }

        if(endTime!=0){
            query.addCriteria(new Criteria("jsrqLong").lt(startTime));
        }
        if(jyyId !=0){
            query.addCriteria(new Criteria("jsrqLong").lt(startTime));
        }
        Criteria criteria2 = new Criteria();
        //检测人等于
        criteria2.orOperator(Criteria.where("respirator.jyyId").is(jyyId), Criteria.where("cylinders.jyyId").is(jyyId));
        query.addCriteria(criteria2);
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName); // 取得满足条件的数据
        return list.size();
    }



    /**
     * 询某条数据的详细信息
     * @return
     */
    public  List<Report> getReportByTYype(int type) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("type").is(type));
        query.with(Sort.by(Sort.Order.asc("addTime")));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        return list;
    }



    /**
     * 询某条数据的详
     * @return
     */
    public  List<Report> getReportByReportProgress(int reportProgress) {
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("reportProgress").is(reportProgress));
        query.addCriteria(new Criteria("type").is(2));
        List<Report> list = mongoTemplate.find(query, Report.class, Report.TableName);
        return list;
    }


}
